<?php

namespace Lib;

class Tree {

	/**
	 *
	 * @param 表名 $sTable        	
	 * @param 父栏目ID $iParentId        	
	 * @param 上级栏目路径 $sPath        	
	 * @param 父栏目ID字段名 $sParentField        	
	 * @param 子栏目ID字段名 $sChildField        	
	 * @param 路径值所用字段名 $sPathValueField        	
	 * @param 路径字段名 $sPathField        	
	 * @param 等级字段名 $sLevelField        	
	 *
	 */
	public function setPath($sTable, $iParentId = 0, $sPath = null, $sParentField = 'parent_id', $sChildField = 'id', $sPathValueField = 'id', $sPathField = 'path', $sLevelField = 'level') {
		$aInfo = o ()->DB->get ( $sTable, array (
				$sChildField => $iParentId 
		) );
		if ($aInfo) {
			if ($sPath == null) {
				$sPath = $aInfo [$sPathField];
			}
		}
		$iLevel = intval ( $aInfo [$sLevelField] );
		if (! $sPath) {
			$sPath = '/';
		}
		$aData = o ()->DB->getAll ( 'category', array (
				$sParentField => $iParentId 
		), array (
				$sChildField,
				$sParentField 
		) );
		if (is_array ( $aData )) {
			foreach ( $aData as $aRow ) {
				$sThisPath = $sPath . $aRow [$sPathValueField] . '/';
				o ()->DB->update ( 'category', array (
						$sPathField => $sThisPath,
						$sLevelField => $iLevel + 1 
				), array (
						$sChildField => $aRow [$sChildField] 
				) );
				$this->setPath ( $sTable, $aRow [$sChildField], $sThisPath, $sParentField, $sChildField, $sPathValueField, $sPathField );
			}
		}
	}

	/**
	 *
	 * @param array $aParam
	 *        	$aParam['table'] 表名
	 *        	$aParam['where'] 搜索条件
	 *        	$aParam['sort'] 搜索排序
	 *        	$aParam['childField'] 子栏目ID字段名
	 *        	$aParam['parentField'] 父栏目ID字段名
	 *        	$aParam['field'] 需返回的字段名
	 * @return array
	 */
	public function getAllData($aParam) {
		if (! is_array ( $aParam ['where'] )) {
			$aParam ['where'] = array ();
		}
		$aParam ['where'] [$aParam ['parentField']] = $aParam ['parentId'];
		$aList = o ()->DB->ls ( array (
				'table' => $aParam ['table'],
				'where' => $aParam ['where'],
				'sort' => $aParam ['sort'],
				'field' => $aParam ['field'],
				'key' => $aParam ['childField'] 
		) );
		$aAll = array ();
		if ($aList ['count']) {
			foreach ( $aList ['items'] as $k => $aRow ) {
				$aAll [$k] = $aRow;
				$aChildParam = $aParam;
				$aChildParam [parentId] = $k;
				$aChild = $this->getAllData ( $aChildParam );
				if ($aChild) {
					$aAll += $aChild;
				}
			}
		}
		return $aAll;
	}

	public function getTreeFromArray($aData, $aDataMap, $iParentId = 0, $iPadding = 0, $bIsRoot = true, $aExcept = array()) {
		if ($iParentId == 0) {
			$sPath = '/';
			$iRootLevel = 0;
		} else {
			$sPath = $aData [$iParentId] [$aDataMap ['path']];
			$iRootLevel = $aData [$iParentId] [$aDataMap ['level']];
		}
		if ($bIsRoot) {
			$iRootLevel ++;
		}
		if ($iPadding) {
			$sPadding = str_repeat ( '&nbsp;', $iPadding );
		}
		$bBegin = false;
		$aArray = array ();
		foreach ( $aData as $iKey => $aRow ) {
			if (preg_match ( '#' . $sPath . '\d+#', $aRow [$aDataMap ['path']] )) {
				$bBegin = true;
				if (in_array ( $aRow [$aDataMap ['id']], $aExcept )) {
					$aExcept [] = $aRow [$aDataMap ['id']];
					continue;
				} else {
					if ($iPadding) {
						$aArray [$aRow [$aDataMap ['id']]] = str_repeat ( $sPadding, $aRow [$aDataMap ['level']] - $iRootLevel ) . $aRow [$aDataMap ['name']];
					} else {
						$aArray [$aRow [$aDataMap ['id']]] = $aRow [$aDataMap ['name']];
					}
				}
			} elseif ($bBegin) {
				break;
			}
		}
		return $aArray;
	}

	public function getTreeFromTable($sTable, $aDataMap, $aReturnMap, $iParentId = 0, $iPadding = 0, $bIsRoot = true, $aExcept = array()) {
	}
}

?>